<template>
  <div class="hero-containe">
    <h1>{{this.id ? '编辑英雄': '创建英雄'}}</h1>
    
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
        <el-tab-pane label="基础信息" name="info">
          <el-form-item label="英雄名称">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="英雄介绍">
            <el-input v-model="model.desc"></el-input>
          </el-form-item>
          <el-form-item label="英雄类型">
            <el-select v-model="category[0]" disabled  style="margin-right:30px">
              <el-option
              v-for="item in category"
              :key="item._id"
              :label="item.name"
              :value="item._id">
              </el-option>
            </el-select>
            <el-select v-model="model.categories" :multiple="true" placeholder="请选择">
              <el-option
                v-for="item in heroCategories"
                :key="item._id"
                :label="item.name"
                :value="item._id">
              </el-option>
            </el-select>
            <el-button type="primary" @click="deleteCategory" v-if="this.id" style="margin-left:30px">去除关联</el-button>
          </el-form-item>
          <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            :action="$http.defaults.baseURL + '/upload'"
            :show-file-list="false"
            :on-success="(res)=>{handleUploadSuccess(res)}">
            <img v-if="model.avatar"  :src="model.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          </el-form-item>
          <el-row :gutter="20">
              <el-col :span="6">
                <el-form-item class="el-center" label="难度">
                  <el-rate
                    v-model="model.scores.survive"
                    show-score
                    :max="10"
                    text-color="#ff9900">
                  </el-rate>
                </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item  class="el-center" label="技能">
                <el-rate
                  v-model="model.scores.skill"
                  show-score
                  :max="10"
                  text-color="#ff9900">
                </el-rate>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item  class="el-center" label="攻击">
                <el-rate
                  v-model="model.scores.attack"
                  show-score
                  :max="10"
                  text-color="#ff9900">
                </el-rate>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="el-center" label="生存">
                <el-rate
                  v-model="model.scores.survive"
                  show-score
                  :max="10"
                  text-color="#ff9900">
                </el-rate>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="技能信息" name="detail">
          <el-form-item >
            <el-button type="text"  @click="addSkill">添加</el-button>
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12" v-for="(item,index) in model.skills" :key="index">
              <el-form-item label="技能名称">
                <el-input placeholder="技能名称" v-model="item.name" ></el-input>
              </el-form-item>
              <el-form-item label="说明">
                <el-input  type="textarea" :rows="3" v-model="item.desc" placeholder="说明"></el-input>
              </el-form-item>
              <el-form-item label="提示">
                <el-input  type="textarea"  :rows="1" v-model="item.tip" placeholder="提示"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="(res)=>{handleUploadSuccess(res,item)}">
                  <img v-if="item.icon" :src="item.icon" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="英雄技巧" name="tips">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="使用技巧">
                <el-input type="textarea" :rows="3" v-model="model.tips.usage"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="对线技巧">
                <el-input type="textarea" :rows="3" v-model="model.tips.battle"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="团战技巧">
                <el-input type="textarea" :rows="3" v-model="model.tips.team"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
    
  </div>
</template>
<script>
export default {
  name: 'HeroEdit',
  props: {
    id: String
  },
  data () {
    return {
      model: {
        scores: {
          diffcult: 0,
          skill: 0,
          attack: 0,
          survive: 0
        },
        tips:{
          usage: '',
          battle: '',
          team: ''
        },
        skills: []
      },
      category: [],
      heroCategories: [],
      activeName: 'info'
    }
  },
  methods: {
    handleTabClick(tab){
      this.activeName = tab.name
    },
    addSkill(){
      this.model.skills.push({})
    },
    async save () {
      let result
      
      if(this.id){
       
        result = await this.$http.put(`/rest/heros/${this.id}`,this.model)
      } else {
        result = await this.$http.post('/rest/heros',this.model)
      }
      
      if(result.data){
        this.$router.push('/heros/list')
        this.$message({
          type: 'success',
          message: '保存成功'
        })
      }
    },
    async fetch () {
      const res = await this.$http.get(`/rest/heros/${this.id}`)
      this.model = res.data
    },
    async fetchCategories () {
      const res = await this.$http.get('/rest/categories',{
        params: {
          $where: "this.parents.length <= 0",
          name: '英雄列表'
        }
      })
      this.category = res.data
      if(this.category){
        const children = this.category[0] && this.category[0].children
        if(children){
          const childrenRes = await this.$http.get(`/rest/categories/ids/${children}`)
          this.heroCategories = childrenRes.data
        }
      }
    },
    handleUploadSuccess (res,item) {
      if(item){
        res.data.url && this.$set(item,'icon',res.data.url)
      }else{
        console.log(res.data.url)
        res.data.url && this.$set(this.model,'avatar',res.data.url)
      }
    }
  },
  created() {
    this.id && this.fetch()
    this.fetchCategories()
  },
}
</script>
<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .el-center{
    display: flex;
    align-items: center;
  }
  .el-center>>>.el-form-item__content{
    margin-left: 0!important;
  }
</style>